<template>
  <div class="serverConfig">
    <div class="page-head">
      <div class="page-bread">
        <div class="bread-prev" @click="back">数据模板</div>
        <div class="bread-divider">/</div>
        <div class="bread-now" >上传服务器配置</div>
      </div>
      <div class="bread-info">
        <span>业务模板名称：{{$route.query.name}}</span>  
        <span class="ml15"> CNC类型：{{$route.query.cnc_name}}</span>
      </div>
      <div class="page-tool">
        <search-input placeholder="服务名称搜索" @search="searchChange" />
        <div class="tool-btn" @click="open('addConfig')">新增服务</div>
      </div>
    </div>
    <div class="page-body">
      <div class="table_data">
        <el-table :data="tableData" stripe border :fit="true" :height="$tableHeight">
          <el-table-column prop="name" label="服务名称" width="180"> </el-table-column>
          <el-table-column prop="url" label="URL"> </el-table-column>
          <el-table-column prop="freq" label="上报频率(s)" width="120"> </el-table-column>
          <el-table-column prop="method" label="上报方式" width="120"> </el-table-column>
          <el-table-column label="操作" align="center" width="160">
            <template slot-scope="{ row }">
              <div class="operation-col">
                <div class="txt-btn ope-btn" @click="toParam(row)">
                  <el-tooltip
                    effect="dark"
                    content="参数配置"
                    placement="top-start"
                  >
                    <img src="~assets/img/setting.png" alt="" />
                  </el-tooltip>
                </div>
                <div class="txt-btn ope-btn" @click="open('updateConfig', row)">
                  <el-tooltip
                    effect="dark"
                    content="修改"
                    placement="top-start"
                  >
                    <img src="~assets/img/edit.png" alt="" />
                  </el-tooltip>
                </div>
                <div class="txt-btn" @click="doDelete(row)">
                  <el-tooltip
                    effect="dark"
                    content="删除"
                    placement="top-start"
                  >
                    <img src="~assets/img/delete.png" alt="" />
                  </el-tooltip>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page-footer up">
        <pagina-tion
          class="page-pager"
          v-show="total >= 0"
          :background="true"
          :total="total"
          :offset.sync="offset"
          :limit.sync="limit"
          @pagination="getChangePage"
          @sizeChange="getSizePage"
        />
      </div>
    </div>

    <!-- 新增服务弹框 -->
    <el-dialog
      :visible.sync="model.visible"
      width="370px"
      :title="model.type === 'addConfig' ? '新增服务' : '修改服务'"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <div class="dialog-body">
        <el-form
          ref="form"
          :model="form"
          label-width="110px"
          label-position="left"
        >
          <el-form-item label="服务名称" class="required">
            <el-input
              v-model="form.name"
              name="serviceName"
              v-validate="'required'"
            />
            <div v-show="errors.has('serviceName')" class="el-form-item__error">
              {{ errors.first("serviceName") }}
            </div>
          </el-form-item>
          <el-form-item label="URL" class="required">
            <el-input 
              v-model="form.url" 
              name="serviceUrl"
              type="textarea" 
              v-validate="'required'"
               />
               <div v-show="errors.has('serviceUrl')" class="el-form-item__error">
              {{ errors.first("serviceUrl") }}
            </div>
          </el-form-item>
          <el-form-item label="上报频率(s)" class="required">
            <el-input 
              v-model.number="form.freq" 
              name="refreq"
              v-validate="'required|onlyRule0'"
            />
               <div v-show="errors.has('refreq')" class="el-form-item__error">
              {{ errors.first("refreq") }}
            </div>
          </el-form-item>
          <el-form-item label="上报方式" class="required">
            <el-select
              v-model="form.method"
              name="method"
              placeholder="请选择上报方式"
              v-validate="'required'"
            >
              <el-option
                v-for="item in methodList"
                :key="item.label"
                :label="item.label"
                :value="item.label"
              >
              </el-option>
            </el-select>
             <div v-show="errors.has('method')" class="el-form-item__error">
              {{ errors.first("method") }}
            </div>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
     <!-- 删除弹窗 -->
    <el-dialog
      :visible.sync="visibility"
      width="430px"
      height="100px"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="dialog-content comfirm-body">
        <img class="comfirm-ico" src="~assets/img/warning.png" />
        <div class="comfirm-txt">
          <div>确定删除该条数据？</div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="visibility = false">取 消</el-button>
        <el-button type="primary" @click="asyncDelete">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
 
 
<script>
import * as api from "@/api";
import { limit, smallLimit } from "@/consts/consts";
import { serviceKey } from "@/consts/formKey";
import searchInput from "@/components/libs/searchInput";
import paginaTion from '@/components/libs/paginaTion';

export default {
  components: {
    searchInput,
    paginaTion
  },
  data() {
    return {
      limit,
      total: 0,
      offset: 0,
      tableData: [],
      cncSel: [],
      model: {
        type: "addConfig",
        visible: false,
      },
      methodList:[
        {
          label:"GET"
        },
        {
          label:"POST"
        },
        {
          label:"PUT"
        },
        {
          label:"DELETE"
        },
      ],
      form: {
        ...serviceKey,
        template_id:this.$route.query.id
      },
      visibility: false,
      groupVisable: false,
      search: '',
    };
  },
  methods: {
    back() {
      this.$router.go(-1);
    },
    searchChange(e) {
      this.search = e;
      this.offset = 0;
      this.getList();
    },
    open(type, row = null) {
      this.$validator.reset();
      this.model.type = type;
      if (row) {
        for (const k in serviceKey) {
          this.form[k] = row[k];
          this.form.template_id = this.$route.query.id
        }
        this.id = row.id;
      }
      this.model.visible = true;
    },
    close() {
      this.model.visible = false;
      this.form = { ...serviceKey };
    },

    // 前往服务器配置页
    toParam(row) {
      this.$router.push({
        name: "paramConfig",
        query: {
          id: row.template_id,
          template_name: this.$route.query.name,
          server_id :row.id,
          name: row.name,
          url: row.url,
          cnc: row.cnc,
          cnc_name: this.$route.query.cnc_name,
        },
      });
    },
  
    submit() {
      this.$validator.validateAll().then((valid) => {
        if (valid) {
          this.addConfigOrEdit();
        } else {
          this.$message.warning("有信息未填写正确");
        }
      });
    },
    getChangePage(val) {
      sessionStorage.setItem('serverCurPage', val.offset)
      this.offset = val.offset;
      this.getList();
    },
    
    getSizePage(val) {
      this.offset = 0
      this.getList();
    },

    doDelete(row) {
      this.id = row.id
      this.visibility = true
    },
    async asyncDelete() {
      const { code } = await api.template(this).deleteConfig(this.id);
      if (code === 0) {
        this.$message.success("操作成功");
        this.id = "";
        this.visibility = false;
        if (this.total % this.limit === 1) {
          if (this.offset >= this.limit) {
            this.offset -= this.limit;
          }
        }
        this.getList();
      }
    },

    // 列表
    async getList() {
      const { code, data, total } = await api.template(this).serverConfigList({
        template_id: this.$route.query.id,
        offset: this.offset,
        limit: this.limit,
        name: this.search
      });
      if (code === 0) {
        this.tableData = data
        this.total = total
      }
    },
    async addConfigOrEdit() {
      let fn = this.model.type
      const { code } = await api.template(this)[fn]({
        template_id: this.$route.query.id,
        ...this.form
      }, this.id);
      if (code === 0) {
        this.$message.success("操作成功");
        this.close();
        this.getList();
      }
    },
  },
   
  created() {
    let curPage = sessionStorage.getItem('serverCurPage')
    this.offset = +curPage
    this.getList()
  },
};

</script>

<style>
</style>